package com.jswlw.controller.admin.biz;

import com.alibaba.fastjson.JSON;
import com.github.pagehelper.Page;
import com.jswlw.controller.BaseController;
import com.jswlw.controller.admin.biz.req.DevTypeRequest;
import com.jswlw.entity.biz.BillSet;
import com.jswlw.entity.biz.DevType;
import com.jswlw.entity.biz.FilterInfo;
import com.jswlw.service.biz.BillSetService;
import com.jswlw.service.biz.DevTypeService;
import com.jswlw.service.biz.FilterInfoService;
import io.swagger.models.auth.In;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import java.util.List;

/**
 * @author 吴维斌 E-mail:wuweibin0817@163.com
 * @version 创建时间：${date} ${time}
 */
@Controller
@RequestMapping("/admin/biz/devType")
public class AdminDevTypeController extends BaseController {

    @Autowired
    DevTypeService service;

    @Autowired
    FilterInfoService filterInfoService;

    @Autowired
    BillSetService billSetService;

    @RequestMapping("/index")
    public String index(Model model) {
        return "biz/devType/index";
    }

    /**
     * 广告列表
     *
     * @param
     * @return
     */
    @RequestMapping("/list")
    @ResponseBody
    public Object list(@RequestBody DevType filter) {
        Page<DevType> page = service.queryPage(filter, filter.getOffset(), filter.getLimit());
        return pageResult(page.getResult(), page.getTotal());
    }

    /**
     * 新增广告
     *
     * @return
     */
    @RequestMapping("/add")
    public ModelAndView add() {
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.addObject("filterInfos", filterInfoService.queryList(new FilterInfo()));
        modelAndView.addObject("billSets", billSetService.queryList(new BillSet()));
        modelAndView.setViewName("biz/devType/edit");
        return modelAndView;
    }

    @RequestMapping("/edit")
    public ModelAndView edit(Integer id) {
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.addObject("info", service.find4Id(id));
        modelAndView.addObject("filterInfos", filterInfoService.queryList(new FilterInfo()));
        modelAndView.addObject("billSets", billSetService.queryList(new BillSet()));
        modelAndView.setViewName("biz/devType/edit");
        return modelAndView;
    }

    /**
     * 保存广告
     *
     * @param
     * @param
     * @return
     */
    @RequestMapping(value = "/save", method = RequestMethod.POST)
    @ResponseBody
    public ModelMap save(DevType request,
                         @RequestParam(value = "filterIds[]", required = false) List<Integer> filterIds,
                         @RequestParam(value = "billSetIds[]", required = false) List<Integer> billSetIds) {
        try {
            request.setFilters(JSON.toJSONString(filterIds));
            request.setBillSets(JSON.toJSONString(billSetIds));
            if (request.getId() == null) {
                service.add(request);
            } else {
                service.update(request);
            }
            return this.successResult(null);
        } catch (Exception e) {
            logger.error("保存失败", e);
            return this.failResult("保存失败" + e.getMessage());
        }
    }

    /**
     * 删除广告
     *
     * @param model
     * @param id
     * @return
     */
    @RequestMapping("/delete")
    @ResponseBody
    public ModelMap delete(Model model, Integer id) {
        try {
            service.delete(id);
            return this.successResult("删除成功");
        } catch (Exception e) {
            logger.error("保存失败", e);
            return this.failResult("删除失败" + e.getMessage());
        }
    }
}
